
package com.ymx.sdk.widget.recyclerview.stickyheader;

import com.ymx.sdk.util.ViewUtils;
import com.ymx.sdk.widget.recyclerview.HFListRecyclerAdapter;
import com.ymx.sdk.widget.recyclerview.ListRecyclerAdapter;
import com.ymx.sdk.widget.recyclerview.adapterItemAnimator.BaseAnimationAdapter;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;
import java.util.Map;

/**
 * A sticky header decoration for android's RecyclerView.
 */
public class StickyHeaderDecoration extends RecyclerView.ItemDecoration {

    private Map<Long, RecyclerView.ViewHolder> mHeaderCache;

    private StickyHeaderAdapter mAdapter;

    private boolean mRenderInline;
    
    private boolean mIsAdapterEmpty=false;

    /**
     * @param adapter the sticky header adapter to use
     */
    public StickyHeaderDecoration(StickyHeaderAdapter adapter) {
        this(adapter, false);
        if (adapter instanceof ListRecyclerAdapter){
          if(((ListRecyclerAdapter) adapter).mList == null ||
                  ((ListRecyclerAdapter) adapter).mList.size()==0){
              mIsAdapterEmpty=true;
          }
        }
    }

    /**
     * @param adapter the sticky header adapter to use
     */
    public StickyHeaderDecoration(StickyHeaderAdapter adapter, boolean renderInline) {
        mAdapter = adapter;
        mHeaderCache = new HashMap<>();
        mRenderInline = renderInline;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        if (mIsAdapterEmpty){
            return;
        }
        int position = parent.getChildAdapterPosition(view);
        /**
         * for headerView
         */
        if (parent.getAdapter() instanceof HFListRecyclerAdapter||parent.getAdapter() instanceof BaseAnimationAdapter) {
            if (ViewUtils.getAdapter(parent).isHeader(position)
                    || ViewUtils.getAdapter(parent).isFooter(position)) {
                outRect.set(0, 0, 0, 0);
                return;
            }

            position = parent.getChildAdapterPosition(view)
                    - (ViewUtils.getAdapter(parent)).getHeaderSize();

        }
        int headerHeight = 0;
        if (position != RecyclerView.NO_POSITION && hasHeader(position)) {
            View header = getHeader(parent, position).itemView;
            headerHeight = getHeaderHeightForLayout(header);
        }
        outRect.set(0, headerHeight, 0, 0);
    }

    /**
     * Clears the header view cache. Headers will be recreated and rebound on
     * list scroll after this method has been called.
     */
    public void clearHeaderCache() {
        mHeaderCache.clear();
    }

    private boolean hasHeader(int position) {
        if (position < 0) {
            return false;
        }
        if (position == 0) {
            return true;
        }

        int previous = position - 1;
        return mAdapter.getHeaderId(position) != mAdapter.getHeaderId(previous);
    }

    private RecyclerView.ViewHolder getHeader(RecyclerView parent, int position) {

        final long key = mAdapter.getHeaderId(position);

        if (mHeaderCache.containsKey(key)) {
            return mHeaderCache.get(key);
        } else {
            final RecyclerView.ViewHolder holder = mAdapter.onCreateHeaderViewHolder(parent);
            final View header = holder.itemView;

            // noinspection unchecked
            mAdapter.onBindHeaderViewHolder(holder, position);

            int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(),
                    View.MeasureSpec.EXACTLY);
            int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(),
                    View.MeasureSpec.UNSPECIFIED);

            int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
                    parent.getPaddingLeft() + parent.getPaddingRight(),
                    header.getLayoutParams().width);
            int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
                    parent.getPaddingTop() + parent.getPaddingBottom(),
                    header.getLayoutParams().height);

            header.measure(childWidth, childHeight);
            header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());

            mHeaderCache.put(key, holder);

            return holder;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mIsAdapterEmpty){
            return;
        }
        final int count = parent.getChildCount();
        for (int layoutPos = 0; layoutPos < count; layoutPos++) {
            final View child = parent.getChildAt(layoutPos);
            int adapterPos = parent.getChildAdapterPosition(child);
            if (parent.getAdapter() instanceof HFListRecyclerAdapter||parent.getAdapter() instanceof BaseAnimationAdapter) {
                if (ViewUtils.getAdapter(parent).isFooter(adapterPos)) {
                    continue;
                }
                adapterPos -= ViewUtils.getAdapter(parent).getHeaderSize();
            }
            if (adapterPos != RecyclerView.NO_POSITION && (layoutPos == 0 || hasHeader(adapterPos))) {
                if (adapterPos < 0) {
                    continue;
                }
                View header = getHeader(parent, adapterPos).itemView;
                c.save();
                final int left = child.getLeft();
                final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
                c.translate(left, top);
                header.draw(c);
                c.restore();
            }
        }
    }

    private int getHeaderTop(RecyclerView parent, View child, View header, int adapterPos,
            int layoutPos) {
        int headerCount = 0;
        if (parent.getAdapter() instanceof HFListRecyclerAdapter||parent.getAdapter() instanceof BaseAnimationAdapter) {
            headerCount = ViewUtils.getAdapter(parent).getHeaderSize();
        }
        int headerHeight = getHeaderHeightForLayout(header);
        int top = ((int) child.getY()) - headerHeight;
        if (layoutPos == 0) {
            final int count = parent.getChildCount();
            final long currentId = mAdapter.getHeaderId(adapterPos);
            // find next view with header and compute the offscreen push if
            // needed
            for (int i = 1; i < count; i++) {
                int adapterPosHere = parent.getChildAdapterPosition(parent.getChildAt(i))
                        - headerCount;
                if (parent.getAdapter() instanceof HFListRecyclerAdapter|| parent.getAdapter() instanceof BaseAnimationAdapter) {
                    if (ViewUtils.getAdapter(parent).isFooter(adapterPosHere
                            + headerCount)) {
                        continue;
                    }
                }
                if (adapterPosHere != RecyclerView.NO_POSITION) {
                    long nextId = mAdapter.getHeaderId(adapterPosHere);
                    if (nextId != currentId) {
                        final View next = parent.getChildAt(i);
                        final int offset = ((int) next.getY())
                                - (headerHeight + getHeader(parent, adapterPosHere).itemView
                                        .getHeight());
                        if (offset < 0) {
                            return offset;
                        } else {
                            break;
                        }
                    }
                }
            }

            top = Math.max(0, top);
        }

        return top;
    }

    private int getHeaderHeightForLayout(View header) {
        return mRenderInline ? 0 : header.getHeight();
    }
}
